//
//  WOHistoryViewController.m
//
//  Created by Olof Millberg on 2010-03-25.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "WOPrototypeAppDelegate.h"
#import "WOHistoryViewController.h"

#import "WOHistoryEntryDetailViewController.h"

#import "WOHistorySection.h"

#import "HistoryEntryCell.h"

#import "WOHistoryEntry.h"

@implementation WOHistoryViewController

#pragma mark Initialization Methods

- (id) init
{
	if (self = [super initWithStyle:UITableViewStylePlain])
	{
		self.title = @"Historik";
		
		UITabBarItem* theItem = [[UITabBarItem alloc] initWithTitle:@"Historik" image:[UIImage imageNamed:@"tabbar_history.png"] tag:2];
		
		self.tabBarItem = theItem;
		[theItem release];
	}
	
	return self;
}

- (void) loadView
{
	[super loadView];
	
	self.tableView.separatorColor = [UIColor groupTableViewBackgroundColor];
	self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
	
	self.tableView.rowHeight = 64;
	
	self.sections = [NSMutableArray array];
	[self.sections addObject:[WOHistorySection historySectionWithList:workOrder.historyEntries]];
}

- (void) viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	[self.tabBarController.navigationItem setRightBarButtonItem:
	 [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose
													target:self 
													action:@selector(showNoteView:)] autorelease] animated:YES];
	[self.tableView reloadData];
}

- (void) viewDidAppear:(BOOL)animated
{
	self.tabBarController.navigationItem.title = self.title;
	[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"currentTab"];
}

- (void) viewWillDisappear:(BOOL)animated
{
	[self.tabBarController.navigationItem setRightBarButtonItem:nil animated:YES];
}

#pragma mark Self Made Methods

- (void) showNoteView:(id)sender
{
	WONewNoteViewController *noteVC = [[WONewNoteViewController alloc] init]; // WithNibName:@"NoteView" bundle:[NSBundle mainBundle]];
	noteVC.delegate = self;
	noteVC.title = @"Ny notis";
	[self.tabBarController.navigationController pushViewController:noteVC animated:YES];
	[noteVC release];
}

#pragma mark -

#pragma mark UINoteView Delegate Methods

- (BOOL) noteView:(WONewNoteViewController *)aNoteView shouldReturnWithText:(NSString *)text
{	
	WOPrototypeAppDelegate *appDelegate = (WOPrototypeAppDelegate *)[[UIApplication sharedApplication] delegate];
	
	WOHistoryEntry *historyEntry = [[WOHistoryEntry alloc] initWithEntryDate:[NSDate date] 
																 description:text 
																   entryType:@"Note"
																	  author:appDelegate.currentUser];
	[workOrder.addedHistoryEntries insertObject:historyEntry atIndex:0];
	[workOrder.historyEntries insertObject:historyEntry atIndex:0];
	[historyEntry release];
	
	return YES;
}

#pragma mark -

#pragma mark UITableView Methods

/* Sets the cell for each row */
- (UITableViewCell *) tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [super tableView:aTableView cellForRowAtIndexPath:indexPath];
	
	WOHistoryEntry *historyEntry = [workOrder.historyEntries objectAtIndex:indexPath.row];
	
	if (historyEntry.author)
		((HistoryEntryCell *)cell).nameLabel.text = [NSString stringWithFormat:@"%@ %@", 
													 historyEntry.author.firstName, historyEntry.author.lastName];
	else
		((HistoryEntryCell *)cell).nameLabel.text = @"";

	NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
	[dateFormatter setDateFormat:@"EEE d MMM YYYY"];
	((HistoryEntryCell *)cell).dateLabel.text = [dateFormatter stringFromDate:historyEntry.entryDate];
	((HistoryEntryCell *)cell).textLabel.text = historyEntry.description;
	
	// TODO: Fix if more entry types are added.
	// This is very ugly, I know.
	((HistoryEntryCell *)cell).entryType = [historyEntry.entryType isEqualToString:@"Note"] ? 0 : 1;

	return cell;
}

/* A cell is tapped */
- (void) tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	WOHistoryEntryDetailViewController *historyEntryVC = [[WOHistoryEntryDetailViewController alloc] 
														  initWithWorkOrder:self.workOrder
														  historyEntry:[workOrder.historyEntries objectAtIndex:indexPath.row]];
	historyEntryVC.title = self.workOrder.address.street;
	
	[self.tabBarController.navigationController pushViewController:historyEntryVC animated:YES];
	
	[historyEntryVC release];
}

@end